package ihm.listeners;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Properties;

import javax.swing.JButton;

import outil.PropertyLoader;

public class ClavierActionListener implements KeyListener {

	JButton btnDown;
	JButton btnUp;
	JButton btnLeft;
	JButton btnRight;
	JButton btnAction;
	JButton btnStart;

	// List<KeyCode> keyCode =

	public ClavierActionListener(JButton btnDown, JButton btnUp,
			JButton btnLeft, JButton btnRight, JButton btnAction,
			JButton btnStart) {
		this.btnDown = btnDown;
		this.btnUp = btnUp;
		this.btnLeft = btnLeft;
		this.btnRight = btnRight;
		this.btnAction = btnAction;
		this.btnStart = btnStart;
	}

	@Override
	public void keyPressed(KeyEvent e) {

		try {

			Properties prop;
			prop = PropertyLoader.load("res/properties/cgi.properties");

			if (e.getKeyCode() == Integer.parseInt(prop
					.getProperty("action.key"))) {
				this.btnAction.doClick(50);
			} else if (e.getKeyCode() == Integer.parseInt(prop
					.getProperty("up.key"))) {
				this.btnUp.doClick(50);
			} else if (e.getKeyCode() == Integer.parseInt(prop
					.getProperty("down.key"))) {
				this.btnDown.doClick(50);
			} else if (e.getKeyCode() == Integer.parseInt(prop
					.getProperty("left.key"))) {
				this.btnLeft.doClick(50);
			} else if (e.getKeyCode() == Integer.parseInt(prop
					.getProperty("right.key"))) {
				this.btnRight.doClick(50);
			} else if (e.getKeyCode() == Integer.parseInt(prop
					.getProperty("start.key"))) {
				this.btnStart.doClick(50);
			}

			/*
			 * switch (e.getKeyCode()) { // Espace case
			 * Integer.parseInt(prop.getProperty("action.key")) : //case 32:
			 * this.btnAction.doClick(50); break; // Haut case 38:
			 * this.btnUp.doClick(50); break; // Bas case 40:
			 * this.btnDown.doClick(50); break; // Droite case 39:
			 * this.btnRight.doClick(50); break; // Gauche case 37:
			 * this.btnLeft.doClick(50); break; // s case 83:
			 * this.btnStart.doClick(50); break; }
			 */

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}
}
